package li.kaeppe.travel.tracker.web.backing;

import java.io.IOException;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import li.kaeppe.travel.tracker.parser.NoTimeStampException;
import li.kaeppe.travel.tracker.parser.ParseException;
import li.kaeppe.travel.tracker.web.bean.UploadFileBean;
import li.kaeppe.travel.tracker.web.service.WayPointUploadService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.oxm.XmlMappingException;

public class DataUploadBacking {
	
	/* **************************************************** */
	/*                      CONSTANTS                       */
	/* **************************************************** */
	
	private static final Log log = LogFactory.getLog(DataUploadBacking.class);
	
	/* **************************************************** */
	/*                     MEMBER FIELDS                    */
	/* **************************************************** */

	private UploadFileBean fileBean;
	private WayPointUploadService wayPointUploadService;
	
	/* **************************************************** */
	/*                      CONSTRUCTOR                     */
	/* **************************************************** */
	
	/* **************************************************** */
	/*                    PUBLIC METHODS                    */
	/* **************************************************** */
	
	public String saveFile() {
		try {
			
				this.getWayPointUploadService().uploadWayPoints(
									this.getFileBean().getFile().getInputStream(), 
									this.getFileBean().getTripId());
				
				this.getWayPointUploadService().updateCalculatedTripData(
									this.getFileBean().getTripId());
				
				this.getFileBean().reset();
			
		} catch (NoTimeStampException e) {
			log.info("Can't save uploaded file [NoTimeStampException]");
			this.getFileBean().setTimeRequired(true);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
			         "Missing time stamp", "The given file doesn't provide time information."));
			return "error";
		} catch (XmlMappingException e) {
			log.error("Can't save uploaded file [XmlMappingException]", e);
			return "error";
		} catch (ParseException e) {
			log.error("Can't save uploaded file [ParseException]", e);
			return "error";
		} catch (IOException e) {
			log.error("Can't save uploaded file [IOException]", e);
			return "error";
		}
		
		return "success";
	}
	
	public String saveFileNoTimeStamp() {
		try {
				this.getWayPointUploadService().uploadWayPoints(
						this.getFileBean().getFile().getInputStream(), 
						this.getFileBean().getTripId(),
						this.getFileBean().getStartDate());
				
				this.getWayPointUploadService().updateCalculatedTripData(
						this.getFileBean().getTripId());
				
				this.getFileBean().reset();
			
		} catch (NoTimeStampException e) {
			log.info("Can't save uploaded file [NoTimeStampException]");
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
			         "Missing time stamp", "The given file doesn't provide time information."));
			this.getFileBean().setTimeRequired(true);
			return "error";
		} catch (XmlMappingException e) {
			log.error("Can't save uploaded file [XmlMappingException]", e);
			return "error";
		} catch (ParseException e) {
			log.error("Can't save uploaded file [ParseException]", e);
			return "error";
		} catch (IOException e) {
			log.error("Can't save uploaded file [IOException]", e);
			return "error";
		}
		return "success";
	}
	
	/* **************************************************** */
	/*                  PROTECTED METHODS                   */
	/* **************************************************** */
	
	/* **************************************************** */
	/*                   PRIVATE METHODS                    */
	/* **************************************************** */
	
	/* **************************************************** */
	/*                  GETTERS AND SETTERS                 */
	/* **************************************************** */
	
	public UploadFileBean getFileBean() {
		return fileBean;
	}

	public void setFileBean(UploadFileBean fileBean) {
		this.fileBean = fileBean;
	}

	public WayPointUploadService getWayPointUploadService() {
		return wayPointUploadService;
	}

	public void setWayPointUploadService(WayPointUploadService wayPointUploadService) {
		this.wayPointUploadService = wayPointUploadService;
	}

}
